package com.geek.factory.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.geek.factory.constant.SuccessConstant;
import com.geek.factory.entity.MaterialSupplier;
import com.geek.factory.result.Result;
import com.geek.factory.service.MaterialSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 原料供应商关联表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-06-23
 */
@RestController
@RequestMapping("/material-supplier")
@Api(tags = "后台系统的 - 原料供应商关联模块")
@Slf4j
public class MaterialSupplierController {

    @Autowired
    private MaterialSupplierService materialSupplierService;

    @ApiOperation("通过ID查询关联信息")
    @GetMapping("/{id}")
    public Result getMaterialSupplierById(@ApiParam("关联ID") @PathVariable Long id) {
        MaterialSupplier materialSupplier = materialSupplierService.getById(id);
        log.info("关联信息: {}", materialSupplier);
        if (materialSupplier != null) {
            return new Result(SuccessConstant.SUCCESS, "查询成功", materialSupplier);
        }
        return new Result(SuccessConstant.FAIL, "关联信息不存在");
    }

    @ApiOperation("新增原料供应商关联")
    @PostMapping
    public Result createMaterialSupplier(@RequestBody MaterialSupplier materialSupplier) {
        boolean exists = materialSupplierService.lambdaQuery()
                .eq(MaterialSupplier::getSupplierId, materialSupplier.getSupplierId())
                .eq(MaterialSupplier::getMaterialId, materialSupplier.getMaterialId())
                .exists();
        if (exists) {
            return new Result(SuccessConstant.FAIL, "该原料已添加到该供应商");
        }
        boolean isSuccess = materialSupplierService.save(materialSupplier);
        log.info("新增关联: {}, 结果: {}", materialSupplier, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "新增成功", materialSupplier);
        }
        return new Result(SuccessConstant.FAIL, "新增失败");
    }
    @ApiOperation("修改关联信息")
    @PutMapping
    public Result updateMaterialSupplier(@RequestBody MaterialSupplier materialSupplier) {
        boolean isSuccess = materialSupplierService.updateById(materialSupplier);
        log.info("修改关联: {}, 结果: {}", materialSupplier, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "修改成功", materialSupplier);
        }
        return new Result(SuccessConstant.FAIL, "修改失败");
    }

    @ApiOperation("根据ID删除关联")
    @DeleteMapping("/{id}")
    public Result deleteMaterialSupplier(@ApiParam("关联ID") @PathVariable Long id) {
        boolean isSuccess = materialSupplierService.removeById(id);
        log.info("删除关联ID: {}, 结果: {}", id, isSuccess);
        if (isSuccess) {
            return new Result(SuccessConstant.SUCCESS, "删除成功");
        }
        return new Result(SuccessConstant.FAIL, "删除失败");
    }

    @ApiOperation("分页查询关联列表")
    @GetMapping("/page")
    public Result getMaterialSupplierPage(
            @RequestParam Integer pageNum,
            @RequestParam Integer pageSize
    ) {
        Page<MaterialSupplier> page = materialSupplierService.page(new Page<>(pageNum, pageSize));
        Map<String, Object> data = new HashMap<>();
        data.put("records", page.getRecords());
        data.put("total", page.getTotal());
        return new Result(SuccessConstant.SUCCESS, "查询成功", data);
    }


}